package cn.entity.img;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class HpBar extends JPanel {
    private int x;
    private int y;
    private int width;
    private int height;
    private int hpBarWidth;
    private int hpBarHeight;

    private BufferedImage[] imgs;
    private BufferedImage img;

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    public HpBar() throws IOException {
        imgs=new BufferedImage[6];
        for(int i=0;i<imgs.length;i++){
            imgs[i]= ImageIO.read(new File("img/Hp/"+i+".jpg"));
            this.x=0;
            this.y=0;
        }
        this.width=imgs[0].getWidth();
        this.height=imgs[0].getHeight();
        this.hpBarWidth=imgs[0].getWidth()-8;
        this.hpBarHeight=imgs[0].getHeight()-10;
    }
    public BufferedImage getImage(int i) {
        return imgs[i];
    }
    public void paint(Graphics g,int i) {
        g.drawImage(getImage(0),getX(),getY(),getWidth(),getHeight(),null);
        g.setColor(Color.RED);
        g.fillRect(4,6,hpBarWidth,hpBarHeight);
    }
    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }
    public boolean setHpBarWidth(int i,int level){
        if(hpBarWidth>0){
            hpBarWidth -= Math.max(i / level, 1);
            return true;
        }
     return false;
    }
    public int getHpBarWidth(){
        return hpBarWidth;
    }
}
